/**
 * @param 控制dom元素类
 */
export function hasClass(el, className) {
  let reg = new RegExp('(^|\\s)' + className + '(\\s|$)')
  return reg.test(el.className)
}

export function addClass(el, className) {
  if (hasClass(el, className)) {
    return
  }
  let newClass = el.className === undefined ? [] : el.className.split(' ')
  newClass.push(className)
  el.className = newClass.join(' ')
}

export function removeClass(el, className) {
  if (!hasClass(el, className)) {
    return
  }
  let currentClass = el.className === undefined ? [] : el.className.split(' ')
  let index = currentClass.indexOf(className)
  currentClass.splice(index, 1)
  el.className = currentClass.join(' ')
}

/**
 * @param 需要判断的元素
 * @returns 判断是否触底
 */
export function handleScrollToBottom(ref) {
  // 滚动高度
  const scrollTop = ref && ref.scrollTop
  // 可视高度
  const clientHeight = ref && ref.clientHeight
  // 页面高度
  const scrollHeight = ref && ref.scrollHeight
  if (scrollTop + clientHeight >= scrollHeight - 10) {
    return true
  }
}
/**
 * 判断值是否为空(undefined, null, 空串)
 * return true 值不为空,反之
 */
export function isExistence(value) {
  if (Object.prototype.toString.call(value) === '[object Array]') {
    return !Object.is(JSON.stringify(value), JSON.stringify([]))
  } else if (Object.prototype.toString.call(value) === '[object Object]') {
    return !Object.is(JSON.stringify(value), JSON.stringify({}))
  } else if (Object.prototype.toString.call(value) === '[object String]') {
    return value !== '' && value !== 'null'
  } else if (Object.prototype.toString.call(value) === '[object Undefined]') {
    return false
  } else if (Object.prototype.toString.call(value) === '[object Null]') {
    return false
  } else {
    return true
  }
}
// 生成uuid
export function uuid() {
  let s = []
  let hexDigits = '0123456789abcdef'
  for (let i = 0; i < 36; i++) {
    s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1)
  }
  s[14] = '4'
  s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1)
  s[8] = s[13] = s[18] = s[23] = '-'
  let uuid = s.join('')
  return uuid
}
